/* internal/js/report/viewForm.js */

function resetFields () {
	document.getElementById('deliveryID').value = '';
	document.getElementById('firstname').value = '';
	document.getElementById('lastname').value = '';
	document.getElementById('phone').value = '';
	document.getElementById('address').innerHTML = '';
	document.getElementById('city').innerHTML = '';
	document.getElementById('province').innerHTML = '';
	document.getElementById('postalCode').innerHTML = '';
	document.getElementById('dwellingType').innerHTML = '';
	document.getElementById('building').innerHTML = '';
	document.getElementById('room').innerHTML = '';
	document.getElementById('deliveryStatus').innerHTML = '';
	document.getElementById('instructions').innerHTML = '';
	
	selectResultRow(0);
}


/* To build result table */

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th>Receiver Name</th>' +
	           '<th>Address</th>' +
	           '<th>Dwelling Type</th>' +
	           '<th>Status</th>' +
	           '<th></th>' +
	           '</tr>';
	return html;
}


function buildTable (deliveries) {
	var html = startResultsTable();
	var count = deliveries.childNodes.length;
	var ids = new Array(count);
	var showOptionAll = document.showOpForm.showOption[0].checked;
	var counter = 0;
	for (var loop = 0; loop < count; loop++) {
		var delivery = deliveries.childNodes[loop];
		var id = delivery.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		var status = delivery.getElementsByTagName('status')[0].childNodes[0].nodeValue;
		
		// check the show option and decide whether to keep the info
		if (showOptionAll || status != 'Done')
		{
			ids[counter++] = id;
			
			var address = delivery.getElementsByTagName('address')[0].childNodes[0].nodeValue;
			var city = delivery.getElementsByTagName('city')[0].childNodes[0].nodeValue;
			var province = delivery.getElementsByTagName('province')[0].childNodes[0].nodeValue;
			var postalCode = delivery.getElementsByTagName('postal')[0].childNodes[0].nodeValue;
			var firstname = delivery.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
			
			var lastname;
			if (delivery.getElementsByTagName('lastname')[0].childNodes.length == 0)
				lastname = '';
			else
				lastname = delivery.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;
			var phone = delivery.getElementsByTagName('phone')[0].childNodes[0].nodeValue;
			var dwelling = delivery.getElementsByTagName('dwelling')[0].childNodes[0].nodeValue;
			
			var roomNo;
			if (delivery.getElementsByTagName('room')[0].childNodes.length == 0)
				roomNo = '';
			else
				roomNo = delivery.getElementsByTagName('room')[0].childNodes[0].nodeValue;

			var buildingName;
			if (delivery.getElementsByTagName('building')[0].childNodes.length == 0)
				buildingName = '';
			else
				buildingName = delivery.getElementsByTagName('building')[0].childNodes[0].nodeValue;
			
			var instruction;
			if (delivery.getElementsByTagName('instruction')[0].childNodes.length == 0)
				instruction = '';
			else
				instruction = delivery.getElementsByTagName('instruction')[0].childNodes[0].nodeValue;
			
			html += addResultRow(id, address, city, province, postalCode, firstname, lastname, phone,
					dwelling, roomNo, buildingName, instruction, status);
		}
	}
	var newIds = new Array(counter);
	for (var i = 0; i < counter; i++)
		newIds[i] = ids[i];

	html += endResultsTable(newIds);
	return html;
}

function addResultRow (id, address, city, province, postalCode, firstname, lastname, phone, dwelling, roomNo, buildingName, instruction, status) {
	var	html =
		'<form id="listDeliveryForm' + id + '">' +
		'<input id="address" value="' + address + '" type="hidden" />' +
		'<input id="city" value="' + city + '" type="hidden" />' +
		'<input id="province" value="' + province + '" type="hidden" />' +
		'<input id="postalCode" value="' + postalCode + '" type="hidden" />' +
		'<input id="firstname" value="' + firstname + '" type="hidden" />' +
		'<input id="lastname" value="' + lastname + '" type="hidden" />' +
		'<input id="phone" value="' + phone + '" type="hidden" />' +
		'<input id="dwelling" value="' + dwelling + '" type="hidden" />' +
		'<input id="room" value="' + roomNo + '" type="hidden" />' +
		'<input id="building" value="' + buildingName + '" type="hidden" />' +
		'<input id="instruction" value="' + instruction + '" type="hidden" />' +
		'<input id="status" value="' + status + '" type="hidden" />' +
		'</form>' +
		'<tr id="listItemRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\')">' +
		'<td>' + firstname + " "+ lastname  + '</td>' +
		'<td>' + address + '</td>' +
		'<td>' + dwelling + '</td>' +
		'<td>' + status + '</td>' +
		'<td><input type="submit"';
	if (status != 'Done')
		html += ' value="Flag as Complete" name="flag" onclick="flagAsComplete(\'' + id + '\')" ></td></tr>';
	else
		html += ' value="Flag as Not Ready" name="flag" onclick="flagAsComplete(\'' + id + '\')" ></td></tr>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}


function selectResultRow (id) {
	var ids = document.getElementById('resultsIds').value.split(',');
	var count = ids.length;
	for (var loop = 0; loop < count; loop++) {
		document.getElementById('listItemRow' + ids[loop]).className = 'searchResultsRow';
	}
	try {
		document.getElementById('listItemRow' + id).className = 'selectedResultsRow';
	} catch (e) {
	}
}

function showResult (id) {
	selectResultRow(id);
	// TODO display roomNo, building
	var frm = document.getElementById('listDeliveryForm' + id);
	
	document.getElementById('deliveryID').value = id;
	document.getElementById('firstname').value = frm.firstname.value;
	document.getElementById('lastname').value = frm.lastname.value;
	document.getElementById('phone').value = frm.phone.value;
	document.getElementById('address').innerHTML = frm.address.value;
	document.getElementById('city').innerHTML = frm.city.value;
	document.getElementById('province').innerHTML = frm.province.value;
	document.getElementById('postalCode').innerHTML = frm.postalCode.value;
	document.getElementById('dwellingType').innerHTML = frm.dwelling.value;
	document.getElementById('building').innerHTML = frm.building.value;
	document.getElementById('room').innerHTML = frm.room.value;
	document.getElementById('deliveryStatus').innerHTML = frm.status.value;
	document.getElementById('instructions').innerHTML = frm.instruction.value;
	
	//clearResults();
}